#!/usr/bin/php -c/etc/gtk/php.ini
<?php

function bob_argc_argv() {

	$a = 0;
	$argcount = 0;
	$return = array();

	$arg = $_SERVER['argv'];
	$argcount = count($arg);
	
	for($a = 1; $a < $argcount; $a++) {
		switch($arg[$a]) {
			case('--prefix'): {
				$return['prefix'] = $arg[++$a];
				break;
			}
			case('--non-linux'): {
				$return['non-linux'] = true;
				break;
			}

			case('--help'): {
				echo(
"Configure Options:
   --prefix=<dir>  :  Installation Prefix
   --non-linux     :  Allow non-Linux config.\n\n"
				); exit(0);
				break;
			}
		}
	}
	
	return $return;
}

if(preg_match('/^Win/i',PHP_OS)) {
	echo("\r\nThis application will not operate on Windows.\r\n\r\n");
	exit(1);
}

$cmd = bob_argc_argv();
if(!array_key_exists('prefix',$cmd)) { $cmd['prefix'] = '/usr'; }

echo("Operating System... ".PHP_OS.".\n");
if(PHP_OS != 'Linux' && !array_key_exists('non-linux',$cmd)) {
	echo("\nThis application was designed to operate in Linux.\n");
	echo("Use --non-linux to allow configuration for other Unixesque\n");
	echo("operating systems.\n\n");
}

echo("Installation Prefix... {$cmd['prefix']}.\n");
if(!is_writable($cmd['prefix'])) {
	echo("You need to be root to complete the install.\n\n");
	$needroot = true;
} else {
	$needroot = false;
}

echo("Testing for PHP-GTK2... ");
$phpgtk_ver = phpversion('php-gtk');
if($phpgtk_ver && preg_match('/^2\./',$phpgtk_ver)) {
	echo("{$phpgtk_ver} Found.\n");
} else {
	echo("Not Found.\n");
	echo("You need to install PHP-GTK2 and make sure it is being loaded\n");
	echo("in the PHP.INI file for your system.");
	exit(1);
}


touch('Makefile');
if(!is_writable('Makefile')) {
	echo("Unable to create Makefile (permission denied).\n");
	exit(1);
}

$fp = fopen('Makefile','w');
fwrite($fp,"
all:
	@echo \"Nothing to do. Run 'make install' to install MkDevDVD.\"
	
install:
	@echo -n \"Installing MkDevDVD to {$cmd['prefix']}... \"
	@cp -rf src/* {$cmd['prefix']}/
	@echo \"Done.\"
	
distclean:
	@rm -f Makefile
"); fclose($fp);

echo("\nYou may now 'make install'".(($needroot)?(' (as root)'):('')).".\n\n");

?>